package io.renren.modules.noc.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.noc.entity.NocTemplateEntity;
import io.renren.modules.noc.service.NocTemplateService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 模板
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-04-03 22:37:46
 */
@RestController
@RequestMapping("noc/noctemplate")
public class NocTemplateController extends BaseController{
    @Autowired
    private NocTemplateService nocTemplateService;

    /**
     * 获取默认的活动
     * @return
     */
    @RequestMapping("/getDefaultTemplateId")
    public R getDefaultTid(){
        Integer tid = getDefaultTemplateId();
        if(tid!=null){
            return R.ok(tid.toString());
        }else{
            return R.error();
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("noc:noctemplate:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = nocTemplateService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/listByRole")
    public R listByRole(){
        List<NocTemplateEntity> list = nocTemplateService.queryByRole();
        return R.ok().put("list",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("noc:noctemplate:info")
    public R info(@PathVariable("id") Integer id){
        NocTemplateEntity nocTemplate = nocTemplateService.getById(id);

        return R.ok().put("nocTemplate", nocTemplate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("noc:noctemplate:save")
    public R save(@RequestBody NocTemplateEntity nocTemplate){
        nocTemplateService.save(nocTemplate);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("noc:noctemplate:update")
    public R update(@RequestBody NocTemplateEntity nocTemplate){
        ValidatorUtils.validateEntity(nocTemplate);
        nocTemplateService.updateById(nocTemplate);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("noc:noctemplate:delete")
    public R delete(@RequestBody Integer[] ids){
        nocTemplateService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/isDefault/{id}")
    @RequiresPermissions("noc:noctemplate:isDefault")
    public R isDefault(@PathVariable("id") Integer id){
        Map<String, Object> params = new HashMap<>();
        params.put("is_default","1");
        List<NocTemplateEntity> list = (List<NocTemplateEntity>) nocTemplateService.listByMap(params);
        for(NocTemplateEntity t:list){
            t.setIsDefault(0);
            nocTemplateService.updateById(t);
        }

        NocTemplateEntity nocTemplate = new NocTemplateEntity();
        nocTemplate.setId(id);
        nocTemplate.setIsDefault(1);
        nocTemplateService.updateById(nocTemplate);
        return R.ok();
    }

}
